//
//  TLTweetLikesView.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/30.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "TLTweetLikesView.h"
#import "TLLikeUser.h"

#define TLTweetLikesMaxCount 8
#define TLLikeUserIconW 30
#define TLLikeUserIconH TLLikeUserIconW
#define TLLikeUserIconMargin 5.0

@interface TLTweetLikesView ()

@property (nonatomic, weak) UIImageView *splitlineImageView;

@property (nonatomic, weak) UIImageView *upArrowIcon;

@property (nonatomic, weak) UIButton *moreButton;

@property (nonatomic, strong) NSMutableArray *iconViewArray;

@end

@implementation TLTweetLikesView

- (NSMutableArray *)iconViewArray
{
    if(!_iconViewArray){
        
        _iconViewArray = [NSMutableArray array];
    }
    return _iconViewArray;
}

- (void)setCommentCount:(NSInteger)commentCount
{
    _commentCount = commentCount;
    
    self.splitlineImageView.hidden = !commentCount;
}

- (void)setLikeUsers:(NSArray *)likeUsers
{
    _likeUsers = likeUsers;
    
    for (int i = 0; i < TLTweetLikesMaxCount; i++) {
        UIImageView *iconView = self.iconViewArray[i];
        if(i < likeUsers.count){ // 显示图片
            
            TLLikeUser *likeUser = likeUsers[i];
            [iconView setCircleHeader:likeUser.avatar];
            iconView.hidden = NO;
        }else{ // 隐藏图片
            
            iconView.hidden = YES;
        }
    }
    
    self.moreButton.hidden = !(likeUsers.count > 8);
}

- (void)addLikesIconView
{
    // 先创建8个控件
    for (int i = 0; i < TLTweetLikesMaxCount; i++) {
        
        UIImageView *iconView = [[UIImageView alloc] init];
        iconView.tag = i;
        [self addSubview:iconView];
        [self.iconViewArray addObject:iconView];
    }
    
    // 分割线
    UIImageView *splitlineImageView = [[UIImageView alloc] initWithImage:[UIImage originImageWithName:@"splitlineImg"]];
    self.splitlineImageView = splitlineImageView;
    [self addSubview:splitlineImageView];
    self.splitlineImageView.hidden = !self.commentCount;
    
    // 向上的箭头图标
    UIImageView *upArrowIcon = [[UIImageView alloc] initWithImage:[UIImage originImageWithName:@"commentOrLikeBeginImg"]];
    [self addSubview:upArrowIcon];
    self.upArrowIcon = upArrowIcon;
    self.upArrowIcon.frame = CGRectMake(30, -7, 15, 7);
    
    UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreButton setTitle:@"..." forState:UIControlStateNormal];
    [moreButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    moreButton.backgroundColor = [UIColor colorWithHexString:@"0xD8DDE4"];
    moreButton.frame = CGRectMake(TLScreenW - 30 - 20 - TLLikeUserIconW, 10, TLLikeUserIconW, TLLikeUserIconH);
    moreButton.layer.cornerRadius = TLLikeUserIconH * 0.5;
    moreButton.layer.masksToBounds = YES;
    [moreButton addTarget:self action:@selector(moreButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:moreButton];
    self.moreButton = moreButton;
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = [UIColor colorWithHexString:@"0xF2F4F6"];
       
        [self addLikesIconView];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    int count = (int)self.iconViewArray.count;
    for (int i = 0; i < count; i++) {
        
        UIImageView *photoView = self.iconViewArray[i];
        photoView.width = TLLikeUserIconW;
        photoView.height = TLLikeUserIconH;
        photoView.x = i * (TLLikeUserIconW + TLLikeUserIconMargin) + 15;
        photoView.y = 10.0;
    }
    
    self.splitlineImageView.frame = CGRectMake(0, self.height - 1, self.width, 1.0);
}

- (void)moreButtonClick
{
    TLLog(@"%s", __func__);
}

@end
